<?php
namespace Home\Controller;
use Think\Controller;
class BackController extends Controller{
 public function __construct(){
        parent::__construct();
        if(!session('?qe_user')){
            $this->error('请先登录在访问！',U('User/login'));
        }
 }
 //相关课程
 public function curname($id){
	 if(!empty($id)){
	 $curriculum = M('curriculum')->where('id='.$id)->find();
	 $name = $curriculum['cname'].'('.$curriculum['money'].')';
	 return $name;
	 }
 }
 
 //相关课程优惠
 public function disname($id){
 	if(!empty($id)){
 	$activity = M('activity')->where('id='.$id)->find();
 	$name = $activity['name'].'('.$activity['amount'].')';
 	return $name;
 	}
 }
 //基本设置
 public function basname($id){
	 if(!empty($id)){
	 $other = M('other')->where('id='.$id)->find();
	 $name = $other['name'];
	 return $name;
	 }
 }
 //试听课名称
 public function classname($id){
 	if(!empty($id)){
 	$classname = M('gradesclass')->where('id='.$id)->find();
 	$name = "已试听(".$classname['classname'].")";
 	return $name;
 	}else{
		$name = "未试听";
		return $name;
	}
 }
 //授课老师
 public function teachername($id){
	if(!empty($id)){
 	$staff = M('staff')->where('id='.$id)->find();
 	$name = $staff['name'];
 	return $name;
	}
 }
 //用户
 public function username($id){
	 if(!empty($id)){
	 $user = M('user')->where('id='.$id)->find();
	 $name = $user['name'];
	 return $name;
	 }
 }
//最新沟通记录
	 public function record_data($id,$key=''){
		 if(!empty($id)){
			 $where['sid'] = array('eq',$id,'and');
			 $where['content'] = array('like','%'.$key.'%','and'); 
			 $record_data = M('record')->where($where)->order('start_time desc')->find();
			 return $record_data;
		}
	 }
// 所有沟通记录
	public function record_list($id){
		if(!empty($id)){
		 $record_data = M('record')->where('sid='.$id)->order('start_time desc')->select();
		 foreach($record_data as $k=>$v){
		 	$record_data[$k]['xs_name'] = $this->username($v['xsid']);
		 }
		return $record_data;
		}
	 }
//所有基本设置
	public function all_other(){
		$other = M('other')->select();
		return $other;
	}
	//所有授课教师
	public function all_staff($xid){
		$all_staff = M('staff')->where('lecture_start =1 and xid='.$xid)->select();
		return $all_staff;
	}
	//所有教室
	public function all_room($xid){
		$all_room = M('room')->where('state =1 and xid='.$xid)->select();
		return $all_room;
	}
	//客户名单获取方式
	public function all_am(){
		$am = M('other')->where('pid=3')->select();
		return $am;
	}
	
	public function getAllMondayOf($start_time = '',$end_time='',$week=''){
		$start_time1 = strtotime($start_time);
		$start_time2 = date('Y-m',$start_time1);
		$start_time3 = strtotime($start_time2);
		$end_time1 = strtotime($end_time);
		$end_time2 = date('Y-m',$end_time1);
		$end_time3 = strtotime($end_time2);
		$monarr = array();
		$monarr[] = $start_time2; 
		while( ($start_time3 = strtotime('+1 month', $start_time3)) <= $end_time3){  
			$monarr[] = date('Y-m',$start_time3); // 取得递增月;   
			
		}
		$mondays = array();
		foreach($monarr as $v){
			$maxDay  = date('t', strtotime($v));
			for($i=1; $i<=$maxDay; $i++){
				if(date('w', strtotime($v."-".$i)) == $week){
					$mondays[] = $v."-".($i>9?'':'0').$i;
				}
			}
		}
		return $mondays;
	}
	public function prDates($start,$end){ // 两个日期之间的所有日期  
		$dt_start = strtotime($start);  
		$dt_end = strtotime($end);  
		while ($dt_start<=$dt_end){  
			$all_time[] = date('Y-m-d',$dt_start);  
			$dt_start = strtotime('+1 day',$dt_start);  
		}  
		return $all_time;
	}  
	public function class_schedule($time,$class_id,$name){
		$schedule = M('schedule');
		$data['time'] = $time;
		$data['class_id'] = $class_id;
		$data['name'] = $name;
		if($schedule->add($data)){
			return true;
		}else{
			return false;
		}
	}
	// 基本设置
	public function all_count($key){
		$basic = M('basic')->find();
		if($key==1){
			return $basic['page_num'];
		}elseif($key==2){
			return $basic['number'];
		}
	}
	//名单冲突登记
	public function errors($student,$xid,$old_xsid,$content){
		$errors = M('errors');
		$data['student'] = $student;
		$data['content'] = $content;
		$data['xsid'] = session('qe_user')['id'];
		$data['old_xsid'] = $old_xsid;
		$data['xid'] = $xid;
		if($errors->add($data)){
			return true;
		}
	}
}